<template>
    <div>
        <div class="container">
            <div class="handle-box">
                <el-input class="handle-input mr10" placeholder="员工工号" v-model="query.name"></el-input>
                <el-button @click="handleSearch" icon="el-icon-search" type="primary">搜索</el-button>
            </div>
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    header-cell-class-name="table-header"
                    ref="multipleTable"
            >
                <el-table-column align="center" label="工号" prop="jobNumber"></el-table-column>
                <el-table-column align="center" label="姓名" prop="staffName"></el-table-column>
                <el-table-column align="center" label="部门" prop="depName"></el-table-column>
                <el-table-column align="center" label="电话" prop="phone"></el-table-column>
                <el-table-column align="center" label="员工职级" prop="roleName"></el-table-column>
                <el-table-column align="center" label="入职时间" prop="joinDate"></el-table-column>
                <el-table-column align="center" label="离职时间" prop="dismissionDate"></el-table-column>
                <el-table-column align="center" label="状态">
                    <template slot-scope="scope">
                        <el-tag
                                :type="scope.row.status==='正常'?'success':(scope.row.status==='离职'?'danger':'')"
                        >{{scope.row.status}}
                        </el-tag>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="操作" width="180">
                    <template slot-scope="scope">
                        <el-button
                                @click="handleEdit(scope.$index, scope.row,scope.row.depName,scope.row.roleName,scope.row.dismissionDate,scope.row.status,)"
                                icon="el-icon-edit"
                                type="text"
                        >编辑
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                        :current-page="currentIndex"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                        background
                        layout="total, prev, pager, next"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog :visible.sync="editVisible" title="修改员工信息" width="30%">
            <el-form :model="form" label-width="70px" ref="form">
                <el-form-item label="部门">
                    <template>
                        <el-select placeholder="请选择" v-model="departName">
                            <el-option
                                    :disabled="item.disabled"
                                    :key="item.departName"
                                    :label="item.departValue"
                                    :value="item.departName"
                                    v-for="item in departments">
                            </el-option>
                        </el-select>
                    </template>
                </el-form-item>
                <el-form-item label="员工职级">
                    <template>
                        <el-select placeholder="请选择" v-model="roleName">
                            <el-option
                                    :disabled="item.disabled"
                                    :key="item.roleName"
                                    :label="item.roleValue"
                                    :value="item.roleName"
                                    v-for="item in staffRoles">
                            </el-option>
                        </el-select>
                    </template>
                </el-form-item>
                <el-form-item label="离职时间">
                    <template>
                        <div class="block">
                            <el-date-picker
                                    placeholder="选择日期"
                                    type="date"
                                    v-model="value1">
                            </el-date-picker>
                        </div>
                    </template>
                </el-form-item>
                <el-form-item label="状态">
                    <template>
                        <el-select placeholder="请选择" v-model="statu">
                            <el-option
                                    :key="item.statu"
                                    :label="item.label"
                                    :value="item.statu"
                                    v-for="item in staffstatus">
                            </el-option>
                        </el-select>
                    </template>
                </el-form-item>
            </el-form>
            <span class="dialog-footer" slot="footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button @click="saveEdit" type="primary">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import { fetchData } from '../../api/index';

    export default {
        name: 'staffManage',
        data() {

            return {
                query: {
                    name: '',
                    //起始下标
                    pageIndex: 0,
                    //每页显示条数，默认为5条
                    pageSize: 5
                },
                staffstatus: [{
                    statu: '正常',
                    label: '正常'
                }, {
                    statu: '离职',
                    label: '离职'
                }],
                departments: [],
                staffRoles: [],
                pickerOptions1: {
                    disabledDate(time) {
                        return time.getTime() > Date.now();
                    },
                    shortcuts: [{
                        text: '今天',
                        onClick(picker) {
                            picker.$emit('pick', new Date());
                        }
                    }, {
                        text: '昨天',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24);
                            picker.$emit('pick', date);
                        }
                    }, {
                        text: '一周前',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', date);
                        }
                    }]
                },
                value1: '',
                departName: '',
                roleName: '',
                statu: '',
                //从服务器获取的数据
                tableData: [],
                tableDatas: [],
                multipleSelection: [],
                delList: [],
                editVisible: false,
                pageTotal: 0,
                currentIndex: 1,
                form: {},
                idx: -1,
                id: -1
            };
        },
        created() {
            let _this = this;
            //点击进入时,获取所有的员工信息
            _this.$axios.get('user/getAllStaff').then(function(response) {
                // console.log('所有员工信息'+response);
                _this.tableDatas = response.data;
                _this.pageTotal = _this.tableDatas.length;
                console.log(_this.tableDatas);
                if (_this.tableDatas.length >= _this.query.pageSize) {
                    for (let i = 0; i < _this.query.pageSize; i++) {
                        _this.tableData.push(_this.tableDatas[i]);
                    }
                } else if (_this.tableDatas.length < _this.query.pageSize) {
                    for (let i = 0; i < _this.tableDatas.length; i++) {
                        _this.tableData.push(_this.tableDatas[i]);
                    }
                }
                console.log(_this.tableData);
                console.log(_this.pageTotal);

            }).catch(error => {
                console.log(error);
                alert('网络错误，暂时不能访问');
            });
        },
        methods: {
            // // 获取 easy-mock 的模拟数据
            // getData() {
            //     fetchData(this.query).then(res => {
            //         console.log(res);
            //         this.tableData = res.list;
            //         this.pageTotal = res.pageTotal || 50;
            //     });
            // },
            // 触发搜索按钮
            handleSearch() {
                console.log(this.query.name);
                console.log(this.tableDatas);
                if (this.query.name != '') {
                    this.tableData = [];
                    for (let i = 0; i < this.tableDatas.length; i++) {
                        if (this.query.name === this.tableDatas[i].jobNumber) {
                            this.tableData.push(this.tableDatas[i]);
                        }
                    }
                    this.pageTotal = this.tableData.length;
                } else {
                    console.log(this.currentIndex);
                    this.tableData = [];
                    let changePageIndex = (this.currentIndex - 1) * this.query.pageSize;
                    console.log('当前页第一条数据的下标' + changePageIndex);
                    let changePageSize = this.tableDatas.length - ((this.currentIndex - 1) * this.query.pageSize);
                    console.log('还有多少条数据没展示' + changePageSize);
                    if (changePageSize > this.query.pageSize) {
                        for (let i = changePageIndex; i < changePageIndex + 5; i++) {
                            this.tableData.push(this.tableDatas[i]);
                        }
                    } else {
                        for (let i = changePageIndex; i < changePageIndex + changePageSize; i++) {
                            this.tableData.push(this.tableDatas[i]);
                        }
                    }
                    this.pageTotal = this.tableDatas.length;
                }
            },
            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            delAllSelection() {
                const length = this.multipleSelection.length;
                let str = '';
                this.delList = this.delList.concat(this.multipleSelection);
                for (let i = 0; i < length; i++) {
                    str += this.multipleSelection[i].name + ' ';
                }
                this.$message.error(`删除了${str}`);
                this.multipleSelection = [];
            },
            // 编辑操作
            handleEdit(index, row) {
                let _this = this;
                console.log('选中的下标为：' + index);
                console.log('选中的员工' + row.jobNumber);
                //点击进入时,获取所有的部门信息
                _this.$axios.get('user/getDepartmentName?kind=' + row.kind).then(function(response) {
                    console.log(response);
                    let department = response.data;
                    _this.departments = [];
                    for (let i = 0; i < department.length; i++) {
                        _this.departments.push(department[i]);
                    }
                    console.log(_this.departments);
                }).catch(error => {
                    console.log(error);
                    alert('网络错误，暂时不能访问');
                });
                _this.$axios.get('user/getRoleName?kind=' + row.kind).then(function(response) {
                    console.log(response);
                    let roleName = response.data;
                    _this.staffRoles = [];
                    for (let i = 0; i < roleName.length; i++) {
                        _this.staffRoles.push(roleName[i]);
                    }
                    console.log(_this.staffRoles);
                }).catch(error => {
                    console.log(error);
                    alert('网络错误，暂时不能访问');
                });
                _this.idx = index;
                _this.form = row;
                _this.editVisible = true;
            },
            // 保存编辑
            saveEdit(idx) {
                let _this = this;
                console.log(_this.form.jobNumber);
                _this.$axios.get('user/updateStaff', {
                    params: {
                        kind: _this.form.kind,
                        jobNumber: _this.form.jobNumber,
                        departName: _this.departName,
                        roleName: _this.roleName,
                        value1: _this.value1,
                        statu: _this.statu
                    }
                }).then(function(response) {
                    console.log('修改后的员工信息');
                    console.log(response);
                    // console.log(_this.idx)
                    // _this.tableData[_this.idx]=response.data;
                    _this.editVisible = false;
                    _this.$message.success(`修改第 ${_this.idx + 1} 行成功`);
                    _this.$set(_this.tableData, _this.idx, response.data);
                }).catch(error => {
                    console.log(error);
                    alert('网络错误，暂时不能访问');
                });


            },
            // 分页导航
            handlePageChange(currentIndex) {
                this.currentIndex = currentIndex;
                console.log(this.currentIndex);
                this.tableData = [];
                let changePageIndex = (currentIndex - 1) * this.query.pageSize;
                console.log('当前页第一条数据的下标' + changePageIndex);
                let changePageSize = this.tableDatas.length - ((currentIndex - 1) * this.query.pageSize);
                console.log('还有多少条数据没展示' + changePageSize);
                if (changePageSize > this.query.pageSize) {
                    for (let i = changePageIndex; i < changePageIndex + 5; i++) {
                        this.tableData.push(this.tableDatas[i]);
                    }
                } else {
                    for (let i = changePageIndex; i < changePageIndex + changePageSize; i++) {
                        this.tableData.push(this.tableDatas[i]);
                    }
                }
                console.log(this.tableData);
            }
        }
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }

    .table {
        width: 100%;
        font-size: 14px;
    }

    .red {
        color: #ff0000;
    }

    .mr10 {
        margin-right: 10px;
    }

    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
</style>
